package com.csw.shuanfa.bitset;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;

public class ShuffListMethodBitSetListRemove {


    public static void main(String[] args) {
        Long start = System.currentTimeMillis();
        int num = (int) Math.pow(10, 4);
        List<String> cardList = new ArrayList<>();
        for (int i = 0; i < Math.pow(10, 6); i++) {
            cardList.add("cardId" + i);
        }


        // 提供一个方法,随机从cardList中取出num数量的元素,并且新列表的元素顺序和保持和原列表一致(即:原本在前的还在前边)
        List<String> randomList = shuffleMethod(cardList, num);
        // //randomList);
        Long end = System.currentTimeMillis();
        //(end - start) + "ms");
    }

    /**
     * 随机拿到配置num数量的数据
     */
    private static List<String> shuffleMethod(List<String> inputList, int num) {

        num = num <= inputList.size() ? num : inputList.size();

        //存下表
        List<Integer> weiZhi = new ArrayList<>();
        for (int i = 0; i < inputList.size(); i++) {
            weiZhi.add(i);
        }
        //存获取的下标，按照先后顺序来
        BitSet bit = new BitSet(num);
        for (int i = 0; i < num; i++) {
            int xiaBiao = (int) (Math.random() * weiZhi.size());
            bit.set(weiZhi.get(xiaBiao));
            weiZhi.remove(weiZhi.get(xiaBiao));
        }
        List<String> resList = new ArrayList<>();

        for (int index = 0; index < inputList.size(); index++) {
            if (bit.get(index)) {
                resList.add(inputList.get(index));
            }
        }
        return resList;
    }

}
